// pages/staff/setamount.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    maxamount: 200,
    sid:'',
    mid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const user = wx.getStorageSync('user');
    this.setData({
      sid: user.id,
      mid: user.merchantid
    });
    this.getConfig();
  },

  getConfig: function(){
    var that = this;
    app.util.request({
      url: 'entry/wxapp/config',
      data: {},
      cachetime: 0,
      success: function (res) {
        if(res.data.errno == 0){
           that.setData({
            maxamount: res.data.data.amount
           });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    });
  },

  subAmount: function(e){
    var that = this;
    var amount = Number(e.detail.value.amount);
    var max = Number(that.data.maxamount);
    var sid = that.data.sid;
    var mid = that.data.mid;
    
    if(amount==''){
      wx.showToast({
        title: '金额不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }

    console.log(typeof amount);
    console.log(typeof max);
    if(amount < max){

      app.util.request({
        url: 'entry/wxapp/entryTicket',
        data: {
            sid: sid,
            mid: mid,
            amount: amount,
            type: 1
        },
        cachetime: 0,
        success: function (res) {
          if(res.data.errno == 0){
            wx.showToast({
              title: '录入成功！',
              icon: 'success',
              duration: 2000
            });
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/staff/index',
              }) 
            }, 2000)  
          }
        },
        fail: function (res) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          });
          return false;
        }
      })
    }else{
      wx.redirectTo({
        url: '/pages/staff/coupon?amount='+amount,
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})